package com.wangyadong.hobby.concurrent;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/10/2420:38
 **/
public class AtomicInteger2 {

    public static void main(String[] args) {
////        AtomicInteger getAndSet = new AtomicInteger(10);
////        int addAndadd = getAndSet.getAndAdd(10);
////        System.out.println(addAndadd);
////        System.out.println(getAndSet.get());
        Map<String, Object> map = new HashMap<>();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        map.put("date", timestamp);
        Date date = new Date(System.currentTimeMillis());
        map.put("sqlDate", date);

//
        System.out.println(StringToDateYYYYMMDD(map.get("date").toString()));

        double v = hoursBetweenDifferenceDay(StringToDateYYYYMMDD("2019-02-17"), StringToDateYYYYMMDD("2019-02-19"));
        System.out.println(v);


    }

    public static java.util.Date StringToDateYYYYMMDD(String strDate) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            java.util.Date date = sDateFormat.parse(strDate);
            return date;
        } catch (ParseException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static double hoursBetweenDifferenceDay(java.util.Date time, java.util.Date time2) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(time);
        long timeInMillis1 = instance.getTimeInMillis();
        instance = null;
        time = null;
        Calendar instance2 = Calendar.getInstance();
        instance2.setTime(time2);
        long timeInMillis2 = instance2.getTimeInMillis();
        instance2 = null;
        time2 = null;
        double hours = (double) ((timeInMillis2 - timeInMillis1) / 1000L / 60L) / 60.0D;
        instance = null;
        instance2 = null;
        return hours;
    }

    public static Map<String, String> sortMapByValueDesc(Map<String, String> oriMap) {
        Map<String, String> sortedMap = new LinkedHashMap<>();
        try {
            if (oriMap != null && !oriMap.isEmpty()) {
                List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet());
                Collections.sort(entryList,
                        new Comparator<Map.Entry<String, String>>() {
                            public int compare(Map.Entry<String, String> entry1,
                                               Map.Entry<String, String> entry2) {
                                int value1 = 0, value2 = 0;
                                try {
                                    value1 = Integer.parseInt(entry1.getValue());
                                    value2 = Integer.parseInt(entry2.getValue());
                                } catch (NumberFormatException e) {
                                    value1 = 0;
                                    value2 = 0;
                                }
                                return value2 - value1;
                            }
                        });
                Iterator<Map.Entry<String, String>> iter = entryList.iterator();
                Map.Entry<String, String> tmpEntry = null;
                while (iter.hasNext()) {
                    tmpEntry = iter.next();
                    sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
                }
            }
        } catch (Exception e) {
        }
        return sortedMap;
    }


}
